package com.qeeyou.apps.android.craftsman.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.MasterInfo;
import com.qeeyou.apps.android.craftsman.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2017/12/17.
 */

public class MasterAdapter extends BaseAdapter {

    private Activity mActivity;
    private List<MasterInfo> masterList;
    private LayoutInflater mLayoutInflater;

    public MasterAdapter(Activity activity, List<MasterInfo> masterList) {
        mActivity = activity;
        mLayoutInflater = LayoutInflater.from(mActivity);
        if(this.masterList == null) {
            this.masterList = new ArrayList<MasterInfo>();
        }
        this.masterList.clear();
        if(masterList != null && masterList.size() > 0) {
            this.masterList.addAll(masterList);
        }
    }

    public void setItems(List<MasterInfo> masterList) {
        if(this.masterList == null) {
            this.masterList = new ArrayList<MasterInfo>();
        }
        this.masterList.clear();
        if(masterList != null && masterList.size() > 0) {
            this.masterList.addAll(masterList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int count = this.masterList != null ? this.masterList.size() : 0;
        return count;
    }

    @Override
    public MasterInfo getItem(int i) {
        if(i < 0 || i >= this.masterList.size()) {
            return null;
        }
        return this.masterList.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        if(view == null) {
            view = mLayoutInflater.inflate(R.layout.view_item_craftsman, null, false);
            holder = new ViewHolder();
            holder.imgvAvatar = (ImageView) view.findViewById(R.id.imgvAvatar4ItemCraftsman);
            holder.txtvName = (TextView) view.findViewById(R.id.txtvName4ItemCraftsman);
            holder.ratingBar = (RatingBar) view.findViewById(R.id.ratingBar4ItemGraftsman);
            holder.txtvGrade = (TextView) view.findViewById(R.id.txtvGrade4ItemCraftsman);
            holder.txtvAddress = (TextView) view.findViewById(R.id.txtvAddress4ItemCraftsman);
            holder.txtvService = (TextView) view.findViewById(R.id.txtvService4ItemCraftsman);
            holder.txtvAge = (TextView) view.findViewById(R.id.txtvAge4ItemCraftsman);
            holder.txtvDistance = (TextView) view.findViewById(R.id.txtvDistance4ItemCraftsman);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        MasterInfo craftsmanInfo = this.masterList.get(position);
        if(craftsmanInfo != null) {
            String masterName = craftsmanInfo.getMastername() != null
                    && !craftsmanInfo.getMastername().trim().equalsIgnoreCase("") ?
                    craftsmanInfo.getMastername() : "[xx]";
            holder.txtvName.setText(masterName);
            holder.txtvAge.setText(craftsmanInfo.getAge() + "岁");
            holder.txtvService.setText("服务: " + craftsmanInfo.getServicenum() + "次");
            holder.txtvGrade.setText(craftsmanInfo.getScore() + "分");
            holder.txtvDistance.setText(craftsmanInfo.getRange());
            holder.txtvAddress.setText(craftsmanInfo.getAddress());
            if(craftsmanInfo.getAvatar() != null) {
                String imageUrl = ImageLoader.getInstance().getImageUrl(craftsmanInfo.getAvatar());
                Glide.with(mActivity).load(imageUrl).into(holder.imgvAvatar);
            } else {
                Glide.with(mActivity).load(R.drawable.ic_master_avatar).into(holder.imgvAvatar);
            }
            if(craftsmanInfo.getScore() != null) {
                holder.ratingBar.setRating(Float.parseFloat(craftsmanInfo.getScore()));
            }

        }
        return view;
    }

    static class ViewHolder {
        ImageView imgvAvatar;
        TextView txtvName;
        RatingBar ratingBar;
        TextView txtvGrade;
        TextView txtvAddress;
        TextView txtvAge;
        TextView txtvService;
        TextView txtvDistance;
    }
}
